<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <!-- <role-search @search="reload" /> -->
      <!-- 数据表格 -->
      <div v-for="(item, i) in gzhLLink"
           :key="i">
        <el-row :gutter="15">
          <el-col v-bind="styleResponsive ? { md: 24 } : { span: 24 }">
            <table style="margin-bottom: 30px"
                   class="ele-table ele-table-border ele-table-medium">
              <colgroup>
                <col width="360" />
                <col />
              </colgroup>
              <thead>
                <tr>
                  <th style="text-align: center">
                    <span style="color: #1890ff; cursor: pointer"
                          @click="onPreview(item)">查看(操作流程)</span>
                  </th>
                  <th style="text-align: center">模板类型({{
                      item.messageType == 2
                        ? '小程序跳转链接'
                        : '公众号跳转链接'
                    }})</th>
                </tr>
              </thead>

              <tr style="height: 80px">
                <td style="text-align: center">菜单栏名称:({{ item.menuName }})</td>
                <td style="text-align: right">

                  <div class="flex">
                    <el-input v-model="item.url"
                              :disabled="true"
                              :placeholder="item.url"></el-input>
                    <el-button type="primary"
                               @click="copyAddress(item.url)">复制</el-button>
                  </div>

                  <!-- <el-form-item :label="item.appletName + 'ID'"> -->

                  <!-- </el-form-item> -->
                </td>
              </tr>
            </table>
          </el-col>
        </el-row>
      </div>
      <!-- <ele-pro-table ref="table"
                     :columns="columns"
                     :datasource="datasource"
                     :selection.sync="selection"
                     height="calc(100vh - 265px)"
                     full-height="calc(100vh - 116px)"
                     tool-class="ele-toolbar-form"
                     row-key="id">

        <template v-slot:toolbar>
          <el-button size="small"
                     type="primary"
                     icon="el-icon-plus"
                     class="ele-btn-icon"
                     @click="openEdit()">
            添加
          </el-button>

        </template>

        <template v-slot:messageType="{ row }">
          <el-tag v-if="row.messageType == 1"
                  type="warning">跳转公众号</el-tag>
          <el-tag v-if="row.messageType == 2"
                  type="success">跳转小程序</el-tag>
        </template>
        <template v-slot:appletName="{ row }">
          <div>{{ row.appletName?row.appletName:'--' }}</div>
        </template>
        <template v-slot:notShow="{ row }">
          <el-switch :active-value="1"
                     :inactive-value="2"
                     v-model="row.notShow"
                     @change="editStatus(row)" />
        </template>


        <template v-slot:discount_money="{ row }">
          <div>
            {{ row.discount_money }}
          </div>
        </template>
        <template v-slot:sort="{ row }">
          <i class="sort-handle el-icon-rank ele-text-secondary"
             style="cursor: move"></i>
          <div v-if="false">{{ row }}</div>
        </template>

        <template v-slot:action="{ row }">
          <el-link type="primary"
                   :underline="false"
                   icon="el-icon-edit"
                   @click="openEdit(row)">
            修改
          </el-link>

          <el-popconfirm class="ele-action"
                         title="确定要删除该条信息？"
                         @confirm="remove(row)">
            <template v-slot:reference>
              <el-link type="danger"
                       :underline="false"
                       icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table> -->
    </el-card>
    <!-- 编辑弹窗 -->
    <!-- <role-edit :data="current"
               :drawerEditBoolen.sync="drawerEditBoolen"
               @done="reload" /> -->
    <!-- 权限分配弹窗 -->
  </div>
</template>

<script>
import { getToken } from '@/utils/token-util';
import {
  userOssSignature,
  qnyOssSignature
} from '@/api/creatCenter/material';
import axios from 'axios';
import {
  accountMenuBarList,
  accountMenuBarDetail,
  accountMenuBarDelete
} from '@/api/accountMenuBar/index';
// import RoleSearch from './components/role-search';
import Sortable from 'sortablejs';
// import RoleEdit from './components/role-edit';
import { carouselList } from './constant/constant';

export default {
  name: 'Recharge',
  components: {
    // RoleSearch,
    // RoleEdit
  },

  data () {
    return {
      carouselList,
      // 表格列配置
      columns: [
        {
          type: 'index',
          columnKey: 'index',
          width: 48,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },
        {
          prop: 'menuName',
          label: '菜单名字',
          // sortable: 'custom',
          width: 105,
          showOverflowTooltip: true
        },
        {
          prop: 'messageType',
          label: '消息类型',
          sortable: 'custom',
          showOverflowTooltip: true,
          width: 185,
          slot: 'messageType'
        },
        {
          prop: 'appletName',
          label: '小程序名称',
          sortable: 'custom',
          showOverflowTooltip: true,
          width: 185,
          align: 'center',
          slot: 'appletName'
        },
        {
          prop: 'url',
          label: '跳转链接',
          sortable: 'custom',
          showOverflowTooltip: true,

          align: 'center'
        },

        {
          columnKey: 'action',
          label: '操作',
          width: 230,
          align: 'center',
          resizable: false,
          slot: 'action',
          hideInSetting: true,
          showOverflowTooltip: true
        }
      ],
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,

      // 是否显示编辑弹窗
      drawerEditBoolen: false,
      // 是否显示导入弹窗
      showAuth: false,
      showChoose: false,
      gzhLLink: []
    };
  },

  mounted () {
    // this.rowDrop();
    this.init();
  },
  computed: {},

  methods: {
    // 复制链接
    copyAddress (val) {
      var copyInput = document.createElement("input"); // 创建元素
      //val是要复制的内容
      copyInput.setAttribute("value", val);
      document.body.appendChild(copyInput);
      copyInput.select();
      try {
        var copyed = document.execCommand("copy");
        if (copyed) {
          document.body.removeChild(copyInput);

          this.$message.success("复制成功");
        }
      } catch {
        this.$message.error("复制失败，请检查浏览器兼容");
      }
    },
    rowDrop () {
      const tbody = document.querySelector('.el-table__body-wrapper tbody');
      const _this = this;
      Sortable.create(tbody, {
        forceFallback: true,
        dragClass: 'drag',
        delay: 100,
        animation: 1000,
        onEnd ({ newIndex, oldIndex }) {
          console.log(newIndex);
          console.log(oldIndex);
          //更换表格数据 后续拖动坐标才会正确
          let data = _this.$refs.table.getData();

          let nid = data[newIndex].id;
          let oid = data[oldIndex].id;

          const currRow = data.splice(oldIndex, 1)[0];
          data.splice(newIndex, 0, currRow);

          console.log(data);

          //   sortChange(data)
          //     .then((msg) => {

          //     })
          //     .catch((e) => {
          //       _this.$refs.table.reload();
          //     });

          //去数据库更换排序

          console.log(_this.$refs.table.getData());
        }
      });
    },
    async init () {
      const res = await accountMenuBarList({
        page: 1,
        limit: 50,
        siteId: this.$store.state.site.info.id
      });
      console.log(res);
      res.list.forEach((item) => {
        item.url = window.location.origin + '/' + item.url
      })

      this.gzhLLink = res.list;
    },

    /* 表格数据源 */
    // datasource({ page, limit, where, order }) {
    //   return accountMenuBarList({
    //     page,
    //     limit,
    //     ...where,
    //     siteId: this.$store.state.site.info.id
    //   });
    // },
    /* 刷新表格 */
    reload (where) {
      this.$refs.table.reload({ page: 1, where: where });
    },
    /* 显示编辑 */
    async openEdit (row) {
      console.log(row);
      if (row?.id) {
        const res = await accountMenuBarDetail(row.id);

        this.current = res;
        // 用户余额管理
        this.drawerEditBoolen = !this.drawerEditBoolen;
      } else {
        this.drawerEditBoolen = !this.drawerEditBoolen;
        this.current = null;
      }
    },

    /* 更改状态 */
    editStatus (row) {
      const loading = this.$loading({ lock: true });
      siteCategoriesModify({
        id: row.id,
        notShow: row.notShow,
        siteId: this.$store.state.site.info.id
      })
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          row.status = !row.status ? 1 : 2;
          this.$message.error(e.message);
        });
    },

    /* 显示分配权限 */
    openAuth (row) {
      this.current = row;
      this.showAuth = true;
    },
    /* 删除 */
    remove (row) {
      const loading = this.$loading({ lock: true });
      accountMenuBarDelete(row.id)
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    removeBatch () {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      console.log(this.selection);
      this.$confirm('确定要删除选中的信息吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          console.log(this.selection.map((d) => d.id));
          siteBatchCategoriesDelete(this.selection.map((d) => d.id))
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => { });
    }
  }
};
</script>

<style lang="less">
.categoryImages {
  width: 50px;
  height: 50px;
}
</style>
